package com.user.details;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

public class UserDetails {
  
  private String name;
  private Integer age=null;
  private String email;
  private Date dob;
  private boolean submitted = false;
  
  private String output;
  private boolean isAgeCorrected = false;
  private String errorMessage;
  private boolean showErrorMessage = false;
  private String nameErrorMessage;
  private boolean showNameErrorMessage = false;
  
  public int calculateAgeFromDOB()
  {
	  Calendar birth = new GregorianCalendar();
	  Calendar today = new GregorianCalendar();
	  int calculatedAge = 0;
	  int factor = 0;

	  Date currentDate = new Date(); // current date

	  birth.setTime(dob);
	  today.setTime(currentDate);

	  if (today.get(Calendar.DAY_OF_YEAR) < birth.get(Calendar.DAY_OF_YEAR))
	  {
		  factor = -1;
	  }
	  
	  calculatedAge = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + factor;
	  return calculatedAge;
  }
  
  public void calculateAgeListener(ActionEvent event)
  {
	  int calculatedAge = calculateAgeFromDOB();
	  if ( event.getComponent().getId().equals("confirm") )
	  {
		  if (calculatedAge != this.age)
		  {
			  this.isAgeCorrected = true;
			  this.output = null;
			  this.age = calculatedAge;
		  }
		  else
		  {
			  this.output="submitted";
		  }
	  }
  }
  

  public void valueChangeInput(ValueChangeEvent event){
      if(event.getComponent().getId().equals("name"))
      {
          if(event.getNewValue().equals("Vampire"))
          {
        	  showNameErrorMessage = true;
              nameErrorMessage = "The name 'Vampire' is not allowed";
              output = null;
          }
         else
         {
        	 this.output="submitted";
         }
     }
  }	
  
  
  public boolean isSubmitted() {
    return submitted;
  }

  public String submitUserDetails() {
	  // stub - true
	  //submitted =true;
    //return "submitted";
	  int calculatedAge = calculateAgeFromDOB();
	  if (calculatedAge != this.age)
	  {
		  this.output = null;
	      this.showErrorMessage = true;
	      errorMessage = "Click Confirm Age button to correct your age";
	  }
	  else
	  {
		  this.output = "submitted";
	  }
	  submitted = true;
	  return this.output;
  }

  public Date getDob() {
    return dob;
  }

  public void setDob(Date dob) {
    this.dob = dob;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  
  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }
  
  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }
  
  public boolean isShowErrorMessage() {
	return showErrorMessage;
  }

  public String getNameErrorMessage() {
	return nameErrorMessage;
  }

  public boolean isShowNameErrorMessage() {
	return showNameErrorMessage;
  }

  public String getErrorMessage() {
	return errorMessage;
  }

  public void setErrorMessage(String errorMessage) {
	this.errorMessage = errorMessage;
  }

  public boolean isAgeCorrected() {
	return isAgeCorrected;
  }
}